import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executors;

public class TomcatServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8899);
        while(true){
            Socket socket = serverSocket.accept();
            Executors.newCachedThreadPool().execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("客户端连接了"+socket.getInetAddress().getHostAddress());
                    byte[] byt = new byte[1024];
                    int len = 0;
                    try {
                        InputStream inputStream = socket.getInputStream();
                        len = inputStream.read(byt);
                        String s = new String(byt,0,len);
                        //c=User&a=login?username=admin&password=admin123
                        int den = s.indexOf('=');
                        int ait = s.indexOf('&');
                        int ait1 = s.indexOf('&',ait+1);
                        int den1=s.indexOf('=',den+1);
                        int den2=s.indexOf('=',den1+1);
                        int den3=s.indexOf('=',den2+1);
                        int wen = s.indexOf('?');
                        String username = s.substring(den2+1,ait1);
                        String password = s.substring(den3+1);
                        String UserClass = s.substring(den+1,ait);
                        String methodString=s.substring(den1+1,wen);
                        Class<?> clazz = Class.forName(UserClass);
                        Method method = clazz.getMethod(methodString, String.class, String.class);
                        String s1 = (String)method.invoke(clazz.newInstance(), username, password);
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(s1.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }finally{
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });

        }
    }
}
